package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.vo.ScanCodeRecordCountVo;
import com.ruoyi.system.domain.vo.YhScanCodeProcessVo;
import com.ruoyi.system.domain.vo.YhScanCodeRecordVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.YhScanCodeRecord;
import com.ruoyi.system.service.IYhScanCodeRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户烟盒扫码记录Controller
 * 
 * @author ruoyi
 * @date 2023-06-06
 */
@Api(tags = "用户烟盒扫码记录管理")
@RestController
@RequestMapping("/yh/scanCode/record")
public class YhScanCodeRecordController extends BaseController
{
    @Autowired
    private IYhScanCodeRecordService yhScanCodeRecordService;

    /**
     * 查询用户烟盒扫码记录列表
     */
    @ApiOperation("获取用户烟盒扫码记录列表")
    @PreAuthorize("@ss.hasPermi('yh:scanCodeRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(YhScanCodeRecord yhScanCodeRecord)
    {
        startPage();
        List<YhScanCodeRecordVo> list = yhScanCodeRecordService.listByAdmin(yhScanCodeRecord);
        return getDataTable(list);
    }

    /**
     * 查询用户烟盒扫码记录列表
     */
    @ApiOperation("用户获取自己的烟盒扫码记录列表")
    @PreAuthorize("@ss.hasPermi('yh:scanCodeRecord:list')")
    @GetMapping("/listByLoginUser")
    public TableDataInfo listByLoginUser(YhScanCodeRecord yhScanCodeRecord)
    {
        startPage();
        yhScanCodeRecord.setMemberId(SecurityUtils.getUserId().toString());
        List<YhScanCodeRecord> list = yhScanCodeRecordService.listByLoginUser(yhScanCodeRecord);
        return getDataTable(list);
    }

    /**
     * 导出用户烟盒扫码记录列表
     */
    @ApiOperation("导出用户烟盒扫码记录列表")
    @PreAuthorize("@ss.hasPermi('yh:scanCodeRecord:export')")
    @Log(title = "用户烟盒扫码记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhScanCodeRecord yhScanCodeRecord)
    {
        List<YhScanCodeRecordVo> list = yhScanCodeRecordService.listByAdmin(yhScanCodeRecord);
        ExcelUtil<YhScanCodeRecordVo> util = new ExcelUtil<YhScanCodeRecordVo>(YhScanCodeRecordVo.class);
        util.exportExcel(response, list, "用户烟盒扫码记录数据");
    }

    /**
     * 获取用户烟盒扫码记录详细信息
     */
    @ApiOperation("获取用户烟盒扫码记录详细")
    @ApiImplicitParam(name = "id", value = "用户烟盒扫码记录ID", required = true, dataType = "String", paramType = "path")
    @PreAuthorize("@ss.hasPermi('yh:scanCodeRecord:list')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(yhScanCodeRecordService.getById(id));
    }

    /**
     * 新增用户烟盒扫码记录
     */
    @ApiOperation("新增用户烟盒扫码记录")
    @PreAuthorize("@ss.hasPermi('yh:scanCodeRecord:add')")
    @Log(title = "用户烟盒扫码记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YhScanCodeRecord yhScanCodeRecord)
    {
        return toAjax(yhScanCodeRecordService.ScanCodeRecord(yhScanCodeRecord));
    }

    /**
     * 修改用户烟盒扫码记录
     */
    @ApiOperation("更新用户烟盒扫码记录")
    @PreAuthorize("@ss.hasPermi('yh:scanCodeRecord:edit')")
    @Log(title = "用户烟盒扫码记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YhScanCodeRecord yhScanCodeRecord)
    {
        return toAjax(yhScanCodeRecordService.updateById(yhScanCodeRecord));
    }

    /**
     * 审核用户烟盒扫码记录
     */
    @ApiOperation("审核用户烟盒扫码记录")
    @PreAuthorize("@ss.hasPermi('yh:scanCodeRecord:edit')")
    @Log(title = "用户烟盒扫码记录", businessType = BusinessType.UPDATE)
    @PutMapping("/process")
    public AjaxResult process(@RequestBody YhScanCodeRecord yhScanCodeRecord)
    {
        return toAjax(yhScanCodeRecordService.process(yhScanCodeRecord));
    }

    /**
     * 批量审核用户烟盒扫码记录
     */
    @ApiOperation("批量审核用户烟盒扫码记录")
    @PreAuthorize("@ss.hasPermi('yh:scanCodeRecord:edit')")
    @Log(title = "批量审核用户烟盒扫码记录", businessType = BusinessType.UPDATE)
    @PutMapping("/batchProcess")
    public AjaxResult batchProcess(@RequestBody YhScanCodeProcessVo vo)
    {
        List<String> ids = vo.getIds();
        Integer state = vo.getState();
        for (String id : ids) {
            YhScanCodeRecord yhScanCodeRecord = new YhScanCodeRecord();
            yhScanCodeRecord.setId(id);
            yhScanCodeRecord.setState(state);
            yhScanCodeRecordService.process(yhScanCodeRecord);
        }
        return success();
    }

    /**
     * 删除用户烟盒扫码记录
     */
    @ApiOperation("删除用户信息")
    @ApiImplicitParam(name = "ids", value = "用户烟盒扫码记录ID集合", required = true, dataType = "String", paramType = "path")
    @PreAuthorize("@ss.hasPermi('yh:scanCodeRecord:remove')")
    @Log(title = "用户烟盒扫码记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids)
    {
        return toAjax(yhScanCodeRecordService.removeByIds(ids));
    }

    /**
     * 获取用户烟盒扫码记录统计
     */
    @ApiOperation("获取用户烟盒扫码记录统计")
    @GetMapping(value = "/scanCodeRecordCount")
    public AjaxResult scanCodeRecordCount()
    {
        List<YhScanCodeRecord> list = yhScanCodeRecordService.list(new LambdaQueryWrapper<YhScanCodeRecord>()
                .eq(YhScanCodeRecord::getMemberId, SecurityUtils.getUserId()));
        ScanCodeRecordCountVo scanCodeRecordCountVo = new ScanCodeRecordCountVo();
        if(list.size() > 0){
            long waitCount = list.stream().filter(v -> v.getState() == 1).count();
            scanCodeRecordCountVo.setWaitCount(waitCount);
            long passCount = list.stream().filter(v -> v.getState() == 2).count();
            scanCodeRecordCountVo.setPassCount(passCount);
            long failCount = list.stream().filter(v -> v.getState() == 3).count();
            scanCodeRecordCountVo.setFailCount(failCount);
        }

        return success(scanCodeRecordCountVo);
    }

}
